package com.yanfan.energy.controller;

import cn.hutool.core.util.StrUtil;
import com.yanfan.common.annotation.Anonymous;
import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.energy.domain.Building;
import com.yanfan.energy.domain.Energyynlx;
import com.yanfan.energy.domain.Workshop;
import com.yanfan.energy.domain.statist.*;
import com.yanfan.energy.service.EnergyBoardService;
import com.yanfan.energy.service.TotalEnergyService;
import com.yanfan.energy.service.WorkshopEnergyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

/**
 * @author caijinkang
 * @date 2022年09月30日 9:09
 */
@RestController
@RequestMapping("/energy")
@Api(tags = "能源系统看板")
public class EnergyBoardController {

    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    @Resource
    private final WorkshopEnergyService workshopEnergyService;

    @Resource
    private final EnergyBoardService energyBoardService;

    @Resource
    private final TotalEnergyService totalEnergyService;

    public EnergyBoardController(WorkshopEnergyService workshopEnergyService, EnergyBoardService energyBoardService, TotalEnergyService totalEnergyService) {
        this.workshopEnergyService = workshopEnergyService;
        this.energyBoardService = energyBoardService;
        this.totalEnergyService = totalEnergyService;
    }

    @GetMapping("/energy-board/total-board")
    @ApiOperation("楼栋费用统计")
    public AjaxResult findBoardBuilding(@ApiParam(name = "range", value = "日期格式") @RequestParam(required = false) String range, @ApiParam(name = "begin", value = "开始日期") @RequestParam(required = false) String begin, @ApiParam(name = "end", value = "结束日期") @RequestParam(required = false) String end, @ApiParam(name = "unit", value = "单位") @RequestParam Integer unit) {
        Map<String, List<DateStatistics>> statistics = energyBoardService.findTotalBoard(range, LocalDate.parse(StrUtil.isNotBlank(range) ? formatter.format(LocalDate.now()) : begin, formatter), LocalDate.parse(StrUtil.isNotBlank(range) ? formatter.format(LocalDate.now()) : end, formatter), unit);

        return AjaxResult.success("查询成功", statistics);
    }

    @GetMapping("/energy-board/total-workshop")
    @ApiOperation("车间能耗排名")
    public AjaxResult findBoardWorkshop(@ApiParam(name = "workshopId", value = "车间id") @RequestParam(required = false) Long workshopId, @ApiParam(name = "floorId", value = "楼层id") @RequestParam(required = false) Integer floorId, @ApiParam(name = "buildingId", value = "楼层id") @RequestParam(required = false) Integer buildingId, @ApiParam(name = "range", value = "日期格式") @RequestParam(required = false) String range, @ApiParam(name = "begin", value = "开始日期") @RequestParam(required = false) String begin, @ApiParam(name = "end", value = "结束日期") @RequestParam(required = false) String end, @ApiParam(name = "unit", value = "单位") @RequestParam Integer unit) {
        Map<String, Map<String, List<DateStatistics>>> statistics = totalEnergyService.findWorkshopCostOf(workshopId, floorId, buildingId, range, LocalDate.parse(StrUtil.isNotBlank(range) ? formatter.format(LocalDate.now()) : begin, formatter), LocalDate.parse(StrUtil.isNotBlank(range) ? formatter.format(LocalDate.now()) : end, formatter), unit);

        return AjaxResult.success("查询成功", statistics);
    }

    @GetMapping("/energy-board/medium-distribute")
    @ApiOperation("能源介质分布")
    public AjaxResult findMediaDistribute(@ApiParam(name = "range", value = "日期格式") @RequestParam(required = false) String range, @ApiParam(name = "begin", value = "开始日期") @RequestParam(required = false) String begin, @ApiParam(name = "end", value = "结束日期") @RequestParam(required = false) String end, @ApiParam(name = "unit", value = "单位") @RequestParam Integer unit) {
        Map<String, List<DateStatistics>> statistics = energyBoardService.findMediaDistribute(range, LocalDate.parse(StrUtil.isNotBlank(range) ? formatter.format(LocalDate.now()) : begin, formatter), LocalDate.parse(StrUtil.isNotBlank(range) ? formatter.format(LocalDate.now()) : end, formatter), unit);

        return AjaxResult.success("查询成功", statistics);
    }

    @GetMapping("/energy-board/total-crest")
    @ApiOperation("波峰波谷统计")
    public AjaxResult findTotalCrest(@ApiParam(name = "range", value = "日期格式") @RequestParam(required = false) String range, @ApiParam(name = "begin", value = "开始日期") @RequestParam(required = false) String begin, @ApiParam(name = "end", value = "结束日期") @RequestParam(required = false) String end, @ApiParam(name = "unit", value = "单位") @RequestParam Integer unit) {
        if (StrUtil.isNotBlank(range)) {
            begin = formatter.format(LocalDate.now());
            end = formatter.format(LocalDate.now());
        }
        Map<String, List<DateStatistics>> statistics = energyBoardService.findTotalCrest(range, LocalDate.parse(StrUtil.isNotBlank(range) ? formatter.format(LocalDate.now()) : begin, formatter), LocalDate.parse(StrUtil.isNotBlank(range) ? formatter.format(LocalDate.now()) : end, formatter), unit);

        return AjaxResult.success("查询成功", statistics);
    }

    @GetMapping("/energy-board/workshop-temperature")
    @ApiOperation("车间温度监控")
    public AjaxResult findTemperature() {
        List<EnvironmentStatics> statistics = energyBoardService.findTemperature();
        return AjaxResult.success("查询成功", statistics);
    }

    @GetMapping("/energy-board/workshop-electricity")
    @ApiOperation("车间电监控")
    public AjaxResult findElectricity() {
        List<EnvironmentStatics> statistics = energyBoardService.findElectricity();
        return AjaxResult.success("查询成功", statistics);
    }

    @GetMapping("/energy-board/count-electricity")
    @ApiOperation("总用电监控")
    public AjaxResult findCountElectricity(@ApiParam(name = "range", value = "日期格式") @RequestParam(required = false) String range, @ApiParam(name = "begin", value = "开始日期") @RequestParam(required = false) String begin, @ApiParam(name = "end", value = "结束日期") @RequestParam(required = false) String end) {
        Map<String, List<DateStatistics>> statistics = energyBoardService.findCountElectricity(range, LocalDate.parse(StrUtil.isNotBlank(range) ? formatter.format(LocalDate.now()) : begin, formatter), LocalDate.parse(StrUtil.isNotBlank(range) ? formatter.format(LocalDate.now()) : end, formatter));

        return AjaxResult.success("查询成功", statistics);
    }

    @GetMapping("/energy-board/floor-information")
    @ApiOperation("楼层信息查询")
    public AjaxResult findFloorInformation() {
        List<Building> statistics = energyBoardService.findFloorInformation();
        return AjaxResult.success("查询成功", statistics);
    }

    @GetMapping("/findAllBuilding")
    @ApiOperation("查询所有楼栋")
    public AjaxResult findAllBuilding() {
        List<Building> statistics = energyBoardService.findAllBuilding();
        return AjaxResult.success("查询成功", statistics);
    }

    @GetMapping("/findAllBuildingId")
    @ApiOperation("查询所有楼栋")
    public AjaxResult findAllBuildingId(Long companId) {
        List<Building> statistics = energyBoardService.findAllBuildingId(companId);
        return AjaxResult.success("查询成功", statistics);
    }

    @Anonymous
    @GetMapping("/companList")
    @ApiOperation("查询所有公司")
    public AjaxResult companList(){
        List<Energyynlx> statistics = energyBoardService.companList();
        return AjaxResult.success("查询成功", statistics);
    }





    @GetMapping("/findAllWorkshopByBuildId")
    @ApiOperation("根据楼栋查车间")
    public AjaxResult findAllWorkshopByBuildId(@ApiParam(name = "buildId", value = "楼栋id") Integer buildId) {
        List<DateStatistics> statistics = energyBoardService.findAllWorkshopByBuildId(buildId);
        return AjaxResult.success("查询成功", statistics);
    }

    @PostMapping("/findAllDeviceByWorkshop")
    @ApiOperation("根据多个车间查设备")
    public AjaxResult findAllDeviceByWorkshop(@ApiParam(name = "workShopNumber", value = "车间查询设备入参") @RequestBody List<Workshop> workshops) {
        return AjaxResult.success("查询成功", energyBoardService.findAllDeviceByWorkshop(workshops));
    }

    @GetMapping("/findEnergyByToday")
    @ApiOperation("查询当日能耗")
    public AjaxResult findEnergyByToday(@ApiParam(name = "workshopId", value = "车间id") @RequestParam(required = false) Long workshopId, @ApiParam(name = "unit", value = "单位类型") @RequestParam Integer unit) {
        DayEnergyStatistics statistics = energyBoardService.findEnergyByToday(workshopId, unit);

        return AjaxResult.success("查询成功", statistics);
    }

    @GetMapping("/findUnitByEnergyTce")
    @ApiOperation("能耗总分析")
    public AjaxResult findUnitOfElectricity(@ApiParam(name = "workshopId", value = "车间id") @RequestParam(required = false) Integer workshopId, @ApiParam(name = "range", value = "日期格式") @RequestParam(required = false) String range, @ApiParam(name = "begin", value = "开始日期") @RequestParam(required = false) String begin, @ApiParam(name = "end", value = "结束日期") @RequestParam(required = false) String end, @ApiParam(name = "unit", value = "单位") @RequestParam Integer unit) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        UnitOfStatistics statistics = energyBoardService.findUnitOfElectricity(workshopId, range, LocalDate.parse(StrUtil.isNotBlank(range) ? formatter.format(LocalDate.now()) : begin, formatter), LocalDate.parse(StrUtil.isNotBlank(range) ? formatter.format(LocalDate.now()) : end, formatter), unit);

        return AjaxResult.success("查询成功", statistics);
    }

    @GetMapping("/findWorkshopEnergyTce")
    @ApiOperation("车间能耗分析")
    public AjaxResult findWorkshopEnergyTce(@ApiParam(name = "workshopId", value = "车间id") @RequestParam(required = false) Integer workshopId, @ApiParam(name = "range", value = "日期格式") @RequestParam(required = false) String range, @ApiParam(name = "begin", value = "开始日期") @RequestParam(required = false) String begin, @ApiParam(name = "end", value = "结束日期") @RequestParam(required = false) String end, @ApiParam(name = "unit", value = "单位") @RequestParam Integer unit) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        WorkShopUnitStatistics statistics = energyBoardService.findWorkshopEnergyTce(workshopId, range, LocalDate.parse(StrUtil.isNotBlank(range) ? formatter.format(LocalDate.now()) : begin, formatter), LocalDate.parse(StrUtil.isNotBlank(range) ? formatter.format(LocalDate.now()) : end, formatter), unit);

        return AjaxResult.success("查询成功", statistics);
    }

    @GetMapping("/SetDayEnergyByHourEnergy")
    @ApiOperation("能源小时表导入能源天表")
    public ResponseEntity<Integer> setDayEnergyByHourEnergy() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        Integer statistics = energyBoardService.setDayEnergyByHourEnergy();

        return new ResponseEntity<Integer>(statistics, HttpStatus.OK);
    }

    @GetMapping("/findAllFloorByBuildingId")
    @ApiOperation("根据楼栋ID查询楼层信息")
    public AjaxResult findAllFloorByBuildingId(@ApiParam(name = "buildId", value = "楼栋Id") @RequestParam(required = false) Integer buildId) {
        List<Building> statistics = energyBoardService.findAllFloorByBuildingId(buildId);
        return AjaxResult.success("查询成功", statistics);
    }

    @GetMapping("/findAllWorkshopByFloorId")
    @ApiOperation("根据楼层ID查询车间信息")
    public AjaxResult findAllWorkshopByFloorId(@ApiParam(name = "buildingId", value = "楼层Id") @RequestParam Integer buildingId) {
        List<Workshop> statistics = energyBoardService.findAllWorkshopByFloorId(buildingId);
        return AjaxResult.success("查询成功", statistics);
    }


    @GetMapping("/getEnergyynlx")
    @ApiOperation("根据楼层ID查询车间信息")
    public AjaxResult getEnergyynlx( Long companId,  Long buildingId) {
        List<Energyynlx> energyynlx = energyBoardService.getEnergyynlx(companId, buildingId);
        return AjaxResult.success("查询成功", energyynlx);
    }

}
